<?php

use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Functions\FunctionsPrint;
use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeName;
use Fisharebest\Webtrees\GedcomTag;
use Fisharebest\Webtrees\Http\RequestHandlers\CopyFact;
use Fisharebest\Webtrees\Http\RequestHandlers\DeleteFact;
use Fisharebest\Webtrees\Http\RequestHandlers\EditName;
use Fisharebest\Webtrees\I18N;

/**
 * @var Fact $fact
 */

$individual = $fact->record();
$tree       = $individual->tree();

// Create a fake record, so we can extract the formatted NAME value from it.
$fake_individual = Registry::individualFactory()->new(
    'xref',
    "0 @xref@ INDI\n1 DEAT Y\n" . $fact->gedcom(),
    null,
    $tree
);
$fake_individual->setPrimaryName(0); // Make sure we use the name from "1 NAME"

$container_class = '';

if ($fact->isPendingDeletion()) {
    $container_class = 'wt-old';
} elseif ($fact->isPendingAddition()) {
    $container_class = 'wt-new';
}

?>
<div class="card <?= $container_class ?>">
    <div class="card-header" role="tab" id="name-header-<?= $fact->id() ?>">
        <a data-toggle="collapse" href="#name-content-<?= $fact->id() ?>" aria-expanded="false" aria-controls="name-content-<?= $fact->id() ?>">
            <?= view('icons/expand') ?>
            <?= view('icons/collapse') ?>
            <span class="label"><?= I18N::translate('Name') ?></span>
            <?= $fake_individual->fullName() ?>
            <?php if ($fact->attribute('TYPE') !== '') : ?>
                —
                <?= GedcomCodeName::getValue($fact->attribute('TYPE'), $fact->record()) ?>
            <?php endif ?>
        </a>
    </div>

    <div id="name-content-<?= $fact->id() ?>" class="card-body collapse" data-parent="#individual-names" aria-labelledby="name-header-<?= $fact->id() ?>">
        <dl class="row mb-0">
            <dt class="col-md-4 col-lg-3"><?= I18N::translate('Name') ?></dt>
            <dd class="col-md-8 col-lg-9"><span dir="auto"><?= e($fact->value()) ?></span></dd>

            <?php preg_match_all('/\n2 (\w+) (.+)/', $fact->gedcom(), $matches, PREG_SET_ORDER) ?>
            <?php foreach ($matches as $key => $match) : ?>
                <?php [, $tag, $value] = $match ?>
                <?php if ($tag !== 'SOUR' && $tag !== 'NOTE') : ?>
                    <dt class="col-md-4 col-lg-3">
                        <?= GedcomTag::getLabel($tag) ?>
                    </dt>
                    <dd class="col-md-8 col-lg-9">
                        <?php if ($tag === 'TYPE') : ?>
                            <?= GedcomCodeName::getValue(e($value), $individual) ?>
                        <?php else: ?>
                            <span dir="auto"><?= e($value) ?></span>
                        <?php endif ?>
                    </dd>
                <?php endif ?>
            <?php endforeach ?>
        </dl>

        <?= FunctionsPrintFacts::printFactSources($tree, $fact->gedcom(), 2) ?>
        <?= FunctionsPrint::printFactNotes($tree, $fact->gedcom(), 2) ?>

        <?php if ($fact->canEdit()) : ?>
            <div class="d-flex">
                <a class="btn btn-link ml-auto" href="<?= e(route(EditName::class, ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) ?>" title="<?= I18N::translate('Edit the name') ?>">
                    <?= view('icons/edit') ?>
                    <span class="sr-only"><?= I18N::translate('Edit the name') ?></span>
                </a>

                <a class="btn btn-link" href="#" data-post-url="<?= e(route(CopyFact::class, ['tree' => $fact->record()->tree()->name(), 'xref' => $fact->record()->xref(), 'fact_id' => $fact->id()])) ?>" title="<?= I18N::translate('Copy') ?>">
                    <?= view('icons/copy') ?>
                    <span class="sr-only"><?= I18N::translate('Copy') ?></span>
                </a>

                <a class="btn btn-link" href="#"
                   data-confirm="<?= I18N::translate('Are you sure you want to delete this fact?') ?>"
                   data-post-url="<?= e(route(DeleteFact::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'fact_id' => $fact->id()])) ?>"
                   title="<?= I18N::translate('Delete this name') ?>">
                    <?= view('icons/delete') ?>
                    <span class="sr-only"><?= I18N::translate('Delete this name') ?></span>
                </a>
            </div>
        <?php endif ?>
    </div>
</div>
